import {Store} from 'iflux2';
import {message} from 'antd'

import RefundSetup from './actor/refund-setup'
import {getSetups, saveBasicSetup, saveOptionSetup} from './webapi'

export default class AppStore extends Store{
  bindActor() {
    return [
      new RefundSetup
    ]
  }
  init = async () => {
    const {data, err} = await getSetups();
    this.dispatch('setup-init', data)
  }
  onBasicSetting = (data) => {
    this.dispatch('handle-basic-setup', data);
  }
  onSave = async () => {
    let {afterSaleOptionInfos, ...params} = this.state().toJS();
    const {err} = await saveBasicSetup(params);
    if(!err){
      message.success('保存成功')
    }
  }
  onCancelEditor = () => {
    this.dispatch('cancel-editor')
  }
  onShowModal = (v) => {
    this.dispatch('editor-contents', {editorInfo: v, modalVisb: true});
  }
  onEditorName = ({name, value}) => {
    if(!(value.length > 5 || value.length < 2)){
      this.dispatch('edit-name', {[name]: value})
    }
  }
  onEditorOption = (data) => {
    this.dispatch('edit-option', data)
  }
  onOptionSave = async (v) => {
    let {editorInfo} = this.state().toJS();
    let {isEdit, ...rest} = editorInfo;
    const {err} = await saveOptionSetup(v || rest);
    if(!err){
      message.success('保存成功')
      this.dispatch('option-edit-sucess', v || rest)
    }
  }
} 